﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_IMPL_PROTOCOL_LIST_H__
#define __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_IMPL_PROTOCOL_LIST_H__

#include "public_page.h"
#include "../../tools/head.h"

namespace xos_protocol
{

    template< class T, class PACKER >
	class public_list : public public_page< T, PACKER >
	{
	public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        typedef public_field< T, PACKER > FIELD;
		typedef public_page< T, PACKER > BASE;
	public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		public_list( xos_box::i_serial * pSerial, xos_box::i_buf * pBuf ) : 
			BASE( pSerial, pBuf )
		{
		}
		~public_list()
		{
		}
	public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int req_compose_with_list( int nCookie, int nCmd, typename T::LIST * pList )
        {
            int ret = 0;
            if( 0 == ret )
            {
                ret = FIELD::req_compose( nCookie, nCmd );
            }
            if( 0 == ret )
            {
                BASE::this_num = ( xos::xos_u16 )pList->size();
                ret = FIELD::pack_this_num();
            }
            if( 0 == ret )
            {
                ret = BASE::compose_list_data( pList );
            }
            return ret;
        }
        int req_dis_compose_with_list( typename T::LIST * pList )
        {
            int ret = 0;
            if( 0 == ret )
            {
                ret = FIELD::req_dis_compose();
            }
            if( 0 == ret )
            {
                ret = FIELD::un_pack_this_num();
            }
            if( 0 == ret )
            {
                ret = BASE::dis_compose_list_data( pList );
            }
            return ret;
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		int req_compose( int nCookie, int nCmd )
		{
			int ret = 0;
			if( 0 == ret )
			{
                ret = BASE::req_compose( nCookie, nCmd );
			}
			return ret;
		}
		int req_dis_compose()
		{
			int ret = 0;
            if( 0 == ret )
            {
                ret = BASE::req_dis_compose();
            }
			return ret;
		}
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int res_compose_with_list( int nCookie, int nCmd, int nRet, typename T::LIST * pList )
        {
            int ret = 0;
            if( 0 == ret )
            {
                ret = FIELD::res_compose( nCookie, nCmd, nRet );
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                BASE::this_num = ( xos::xos_u16 )pList->size();
                ret = FIELD::pack_this_num();
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = BASE::compose_list_data( pList );
            }
            return ret;
        }
        int res_dis_compose_with_list( int & nRet, typename T::LIST * pList )
        {
            int ret = 0;
            if( 0 == ret )
            {
                ret = FIELD::res_dis_compose( nRet );
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = FIELD::un_pack_this_num();
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = BASE::dis_compose_list_data( pList );
            }
            return ret;
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		int res_compose( int nCookie, int nCmd, int nRet )
		{
			int ret = 0;
			if( 0 == ret )
			{
                ret = BASE::res_compose( nCookie, nCmd, nRet );
			}
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = BASE::pack_list_info();
            }
			return ret;
		}
        int res_compose_data( T * pT )
        {
            int ret = 0;
            if( 0 == ret )
            {
                ret = BASE::pack_data( pT );
            }
            return ret;
        }
		int res_dis_compose( int & nRet, typename T::LIST * pList )
		{
            int ret = 0;
            if( 0 == ret )
            {
                ret = BASE::res_dis_compose( nRet );
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = BASE::un_pack_list_info();
            }
            if( ( 0 == ret ) && ( 0 == nRet ) )
            {
                ret = BASE::dis_compose_list_data( pList );
            }
            return ret;
		}
	};

}

#endif // __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_IMPL_PROTOCOL_LIST_H__
